package com.cn.crmsystem.controller.systemController;

import com.cn.crmsystem.pojo.Login;
import com.cn.crmsystem.pojo.ResultJson;
import com.cn.crmsystem.pojo.User;
import com.cn.crmsystem.service.systemService.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@Api(value = "登录验证", tags = "用户登录验证API")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ApiOperation(value = "登录验证接口", notes = "员工登录验证账号可否登录",httpMethod = "POST", response = ResultJson.class)
    public ResultJson authLogin(@RequestParam
                                    @ApiParam(name = "username",value = "用户名")String username,
                                @RequestParam
                                    @ApiParam(name = "password",value = "密码")String password){
        System.out.println("-----登录验证-----");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        //提交实体信息
        Subject subject = SecurityUtils.getSubject();
        System.out.println((Login)subject.getPrincipal());
        String msg = "";
        //判断用户是否登录
        if (!subject.isAuthenticated()){
            //收集实体信息
            System.out.println("11111111111111111111111111");
            UsernamePasswordToken upToken = new UsernamePasswordToken(username,password);
            //upToken.setRememberMe(true);
            try {
                //login方法提交认证，自己定义了realm类，跳转执行进行认证
                subject.login(upToken);
                //随机产生一个token
                String token = UUID.randomUUID().toString();
                //将信息保存到redis中，key为用户，value值为token
                if (loginService.saveToken(username,token)){
                    //查询用户信息
                    System.out.println("111111111111111"+(Login)subject.getPrincipal());
                    Login login = (Login)SecurityUtils.getSubject().getPrincipal();
                    return new ResultJson().getSuccess("登录成功",token,login);
                }else {
                    return new ResultJson().getFail("网络异常,请稍后重试(redis无法访问)");
                }
            } catch (IncorrectCredentialsException ice){
                msg = "用户名/密码不正确";
            } catch (LockedAccountException lae) {
                msg = "账户已被冻结";
            } catch (UnknownAccountException uae) {
                msg = "账户不存在";
            } catch (AuthenticationException ae) {
                System.out.println("==============" + ae);
                msg = ae.getMessage();
            }
        }
        return new ResultJson().getFail(msg);
    }

    @ApiOperation(value = "退出登录接口", notes = "用户登出",httpMethod = "POST", response = ResultJson.class)
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public ResultJson logout(){
        System.out.println("-------------logout-----");
        return loginService.del();
    }

    @ApiOperation(value = "修改密码接口",notes = "修改密码",httpMethod = "POST", response = ResultJson.class)
    @RequestMapping(value = "/updateLoginPassword", method = RequestMethod.POST)
    public ResultJson updatePassword(@RequestBody
                                         @ApiParam(name = "login", value = "登录类对象,传password和newPassword") Login login){
        System.out.println("------修改密码----------");
        login = (Login)SecurityUtils.getSubject().getPrincipal();
//        System.out.println("-=-=-=-=-==-=-=="+login);
//        Login login = new Login();
//        login.setUser_id(1);
        System.out.println("+++++++++++++++++++++++++" + login);
        return loginService.updatePassword(login);

    }

}
